library(ComplexHeatmap)
library(circlize)
library(RColorBrewer)
library(dendextend)
library(dendsort)
library(dendextend)
library(gridBase)


mat <- rbind(
  cbind(matrix(
    rnorm(50*5, mean = 1), nr = 50), 
    matrix(rnorm(50*5, mean = -1), nr = 50)
  ),
  cbind(matrix(
    rnorm(50*5, mean = -1), nr = 50), 
    matrix(rnorm(50*5, mean = 1), nr = 50)
  )
)
rownames(mat) <- paste0("R", 1:100)
colnames(mat) <- paste0("C", 1:10)
mat <- mat[sample(100, 100), ]
split <- sample(letters[1:5], 100, replace = TRUE)
split <- factor(split, levels = letters[1:5])
col_fun <- colorRamp2(c(-2, 0, 2), c("#fc8d59", "#ffffbf", "#91bfdb"))
##############################
circos.clear()
circos.par(gap.after = c(2, 2, 2,2, 10))
circos.heatmap(
  mat, split = split, col = col_fun,
  track.height = 0.4
)
circos.track(
  track.index = get.current.track.index(), 
  panel.fun = function(x, y) {
    if (CELL_META$sector.numeric.index == 5) {
      # 在最后一个扇形中
      cn = colnames(mat)
      n = length(cn)
      circos.text(
        rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"),
        1:n - 0.5, cn, cex = 0.5, adj = c(0, 0.5),
        facing = "inside"
      )
    }
  }, 
  bg.border = NA
)
circos.clear()
